package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.model.constant.ModelConst;
import com.guodi.model.entity.MdlControlMeasures;
import com.guodi.model.mapper.MdlControlMeasuresMapper;
import com.guodi.model.service.IMdlControlMeasuresService;
import com.guodi.model.utils.ConvertUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @描述： 决策评估-管控措施服务类
 * @作者: 代鹏刚
 * @日期: 2021-04-25
 */
@Service
public class MdlControlMeasuresServiceImpl extends ServiceImpl<MdlControlMeasuresMapper, MdlControlMeasures> implements IMdlControlMeasuresService{

    @Override
    public List<MdlControlMeasures> listByEntity(Page page, MdlControlMeasures mdlControlMeasures) {
        return baseMapper.listByEntity(page, mdlControlMeasures);
    }

    @Override
    public List<Map<String, String>> analysisJson(String jsonResult, String divisionCode) {
        // 转换成Json对象
        JSONObject dataJson = JSONObject.fromObject(jsonResult);
        // 定义中间数据对象
        List<Map<String, String>> mapList = new ArrayList<>();
        // 该模型结果约定：省市县三级必含有多个结果，乡镇社区只包含一个结果
        String divCode = divisionCode.replaceAll("0+$", "");
        // 将JSON转换成数组
        String jsonString = "[" + dataJson.getJSONObject(ModelConst.MODEL_LEVEL_ROOTGROUPITEM).toString() + "]";
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        // 根据行政区划获取StatisticInformation中的值
        JSONObject jsonObject = recursionJson(jsonArray,divisionCode);
        if(null == jsonObject){
            return mapList;
        }
        // 组装数据
        if(jsonObject.containsKey(ModelConst.MODEL_VALUE_TYPE_CURRENT)){
            JSONObject dataObject = jsonObject.getJSONObject(ModelConst.MODEL_VALUE_TYPE_CURRENT);
            // 循环遍历所有的key，获取对应的value
            for (Iterator<String> iterator = dataObject.keys(); iterator.hasNext();) {
                String key = iterator.next();
                // 过滤掉合计
                if(!key.equals("$合计$")){
                    Map<String, String> map = new HashMap();
                    JSONObject object = dataObject.getJSONObject(key);
                    map.put("name", key);
                    map.put("value", ConvertUtil.formatNumber(Double.valueOf(object.getString("Area_Value")), 2));
                    map.put("unit", StringUtils.isNotBlank(object.getString("Area_Unit")) ? object.getString("Area_Unit") : null);
                    mapList.add(map);
                }
            }
        }
        return mapList;
    }

    private JSONObject recursionJson(JSONArray jsonArray, String divisionCode){
        // 递归遍历获取到对应行政区划下的数据
        for(int i = 0; i < jsonArray.size(); i++){
            if(jsonArray.getJSONObject(i).get(ModelConst.MODEL_FEATURE_DIVISION_GROUPITEMCODE).equals(divisionCode)){
                return (JSONObject) jsonArray.getJSONObject(i).get(ModelConst.MODEL_STATISTICAL_STATISTICINFORMATION);
            }else {
                JSONObject jsonObject = recursionJson(jsonArray.getJSONObject(i).getJSONArray(ModelConst.MODEL_LEVEL_SUBLEVEL1GROUPITEM),divisionCode);
                if(jsonObject != null){
                    return jsonObject;
                }
            }
        }
        return null;
    }
}